package com.inews.demo3;

import java.net.InetSocketAddress;

import org.apache.log4j.Logger;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

public class SmsClient {

	private static Logger logger = Logger.getLogger(SmsClient.class);
	private static String HOST = "127.0.0.1";
	private static int PORT = 3005;

	public static void main(String[] args) {
		// 创建一个非阻塞的客户端程序
		IoConnector connector = new NioSocketConnector();
		// 设置链接超时时间
		connector.setConnectTimeout(30000);
		// 设置过滤器
		connector.getFilterChain().addLast("codec",
				new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
		// 添加业务逻辑处理器类
		connector.setHandler(new SmsClientHandler());

		IoSession session = null;
		try {
			ConnectFuture future = connector.connect(new InetSocketAddress(
					HOST, PORT));// 创建连接
			future.awaitUninterruptibly();
			session = future.getSession();// 获得session

			// 发送的内容
			SmsVo vo = new SmsVo();
			vo.setSendPhone("13681803609");
			vo.setReceivePhone("13721427169");
			vo.setMessage("测试发送短信，这个是短信信息哦，当然长度是有限制的哦....");
			
			session.write(vo);// 发送给移动服务端

		} catch (Exception e) {
			logger.error("客户端链接异常...", e);
		} finally {
			session.getCloseFuture().awaitUninterruptibly();// 等待连接断开
			connector.dispose();
		}

	}

}
